package ui.magic.magicui.container.View;

import android.content.Context;
import android.media.Image;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import ui.magic.magicui.R;
import ui.magic.magicui.container.Base.MagicInterface;
import ui.magic.magicui.container.Linear.LinearMagicNull;
import ui.magic.magicui.container.Linear.LinearMagicUi;

/**
 * Created by liushiyin on 2017/10/22.
 */

public class TopBar extends LinearMagicUi implements View.OnTouchListener{

    private LinearMagicNull nullLeft;
    private LinearMagicNull nullRight;
    private View left;
    private TextView titleText;
    private View right;

    public TopBar(Context context) {
        super(context);
        init();
    }

    public TopBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TopBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        setHorizontal();
        this.setBackgroundResource(R.color.colorAccent);
        nullLeft=new LinearMagicNull(getContext());
        nullRight=new LinearMagicNull(getContext());
        titleText=new TextView(getContext());
        titleText.setGravity(Gravity.CENTER);
        left=new View(getContext());
        left.setBackgroundResource(R.drawable.topback);
        right=new View(getContext());
        right.setBackgroundResource(R.drawable.topmenu);
        this.addChildView(nullLeft,5,100);
        this.addChildView(left,10,80);
        this.addChildView(titleText,70,80);
        this.addChildView(right,10,80);
        this.addChildView(nullRight,5,100);
        left.setOnTouchListener(this);
        right.setOnTouchListener(this);
    }

    public void changeLeftAndRightSize(float perWidth,float perHeight) {
        this.changeChildSize(left, perWidth, perHeight);
        this.changeChildSize(right,perWidth,perHeight);
        this.changeChildSize(titleText,100-perWidth*2,80);
    }

    public void setTitle(String title){
        titleText.setText(title);
    }

    public void setBackgroundStyle(int resid){
        this.setBackgroundResource(resid);
    }

    public void setTopBarBackgroundColor(int color){
        this.setBackgroundColor(color);
    }

    public View getLeftView(){
        return left;
    }

    public View getRightView(){
        return right;
    }

    public void setLeft(View view){
        clear();
        left=view;
        left.setOnTouchListener(this);
        reSet();
    }

    public void setRight(View view){
        clear();
        right=view;
        right.setOnTouchListener(this);
        reSet();
    }

    private void reSet(){
        this.addChildView(nullLeft,5,100);
        this.addChildView(left,10,80);
        this.addChildView(titleText,70,80);
        this.addChildView(right,10,80);
        this.addChildView(nullRight,5,100);
    }

    private void clear(){
        this.removeChildView(nullLeft);
        this.removeChildView(left);
        this.removeChildView(titleText);
        this.removeChildView(right);
        this.removeChildView(nullRight);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            v.setScaleX(1.3f);
            v.setScaleY(1.3f);
        }else if(event.getAction()==MotionEvent.ACTION_UP){
            v.setScaleX(1.0f);
            v.setScaleY(1.0f);
        }
        return true;
    }
}
